package com.rovinj_server.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.rovinj_server.R;
import com.rovinj_server.adapter.FragmentTaskSkedsubAdapter;
import com.rovinj_server.db.DBManager;
import com.rovinj_server.entity.InventoryTaskSub;
import com.rovinj_server.ui.KFDActivity;
import com.rovinj_server.ui.TaskSaninngActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.rovinj_server.R.raw.msg;

/**
 * Created by Administrator on 2017/5/11.
 */

public class CheckTaskFragment extends Fragment {
    public Context mContext;
    @Bind(R.id.list_view)
    ListView mListView;
    @Bind(R.id.butt_quan_bu)
    Button mButtQuanBu;
    @Bind(R.id.but_wei_)
    Button mButWei;
    @Bind(R.id.but_yi_wang)
    Button mButYiWang;
    @Bind(R.id.ll_butt)
    LinearLayout mLlButt;
    private View mView;
    private DBManager mDbManager;
    private List<InventoryTaskSub> mTaskSubList = new ArrayList<>();
    private List<InventoryTaskSub> mTaskKO = new ArrayList<>();
    private List<InventoryTaskSub> mTaskNO = new ArrayList<>();
    private FragmentTaskSkedsubAdapter mAdapter;
    private String mId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = getActivity();

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = View.inflate(mContext, R.layout.fragment_checj, null);
        }
        ButterKnife.bind(this, mView);
        init();
        return mView;
    }

    private void init() {
        SharedPreferences sp = mContext.getSharedPreferences("FWDID", Context.MODE_PRIVATE);
        mId = sp.getString("id", "");
        mDbManager = new DBManager(mContext);
        mDbManager.copyDBFile();
        mTaskSubList = mDbManager.findTaskSubList();
        if (mTaskSubList != null && mTaskSubList.size() > 0) {
            for (int i = 0; i < mTaskSubList.size(); i++) {
                String state = mTaskSubList.get(i).getState();
                if (state != null && state.equals("完成")) {
                    mTaskKO.add(mTaskSubList.get(i));
                } else {
                    mTaskNO.add(mTaskSubList.get(i));
                }
            }
        }
        mAdapter = new FragmentTaskSkedsubAdapter(mContext, mTaskSubList);
        mListView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();

    }


    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.butt_quan_bu, R.id.but_wei_, R.id.but_yi_wang, R.id.but_tiao_jiao})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.butt_quan_bu:
                if (mTaskSubList != null && mTaskSubList.size() > 0) {
                    mAdapter.setListDataBean(mTaskSubList);
                    mAdapter.notifyDataSetChanged();
                } else {
                    Toast.makeText(mContext, "暂无任务", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.but_wei_:
                if (mTaskNO != null && mTaskNO.size() > 0) {
                    mAdapter.setListDataBean(mTaskNO);
                    mAdapter.notifyDataSetChanged();
                } else {
                    Toast.makeText(mContext, "暂无任务", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.but_yi_wang:
                if (mTaskKO != null && mTaskKO.size() > 0) {
                    mAdapter.setListDataBean(mTaskKO);
                    mAdapter.notifyDataSetChanged();
                } else {
                    Toast.makeText(mContext, "暂无任务", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.but_tiao_jiao:

                Toast.makeText(mContext, "提交", Toast.LENGTH_SHORT).show();
                new  MyThread("dsa").start();

                break;
        }
    }



    public  Handler myHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what){
                case 0:
                    String obj = (String) msg.obj;
                    Toast.makeText(mContext, ""+obj, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    Socket socket = null;
    String buffer = "";
    class MyThread extends Thread {
        public String txt1;

        public MyThread(String str) {
            txt1 = str;
        }

        @Override
        public void run() {
            // 定义消息
            List<InventoryTaskSub> taskSubList = mDbManager.findTaskSubList();
            Map map = new HashMap();
            map.put("state", "1");
            map.put("mTaskSubList", taskSubList);
            final String postData = JSON.toJSONString(map);
            final String str = postData;
            Message msg = new Message();
            msg.what = 0;
            Bundle bundle = new Bundle();
            bundle.clear();
            try {

                // 连接服务器 并设置连接超时为5秒
                socket = new Socket();
                socket.connect(new InetSocketAddress(mId, 30000), 1000);
                // 获取输入输出流

                BufferedReader bff = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // 读取发来服务器信息
                String line = null;
                buffer = "";
                while ((line = bff.readLine()) != null) {
                    buffer = line + buffer;
                }
                OutputStream ou = socket.getOutputStream();
                // 向服务器发送信息
                ou.write(str.getBytes("UTF-8"));
                ou.flush();
                bundle.putString("msg", buffer.toString());
                msg.setData(bundle);
                //  发送消息 修改UI线程中的组件
                myHandler.sendMessage(msg);
                // 关闭各种输入输出流
                bff.close();
                ou.close();
                socket.close();
            } catch (SocketTimeoutException aa) {
                // 连接超时 在UI界面显示消息
                bundle.putString("msg", "失败=======");
                msg.setData(bundle);
                // 发送消息 修改UI线程中的组件
                myHandler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
